<?php
/**
 * @package Model
 * @access public
 * @filesource
 */
abstract class Model {
    private static $MESES = array("Jan" => "01", "Feb" => "02", "Mar" => "03",
                                  "Apr" => "04", "May" => "05", "Jun" => "06",
                                  "Jul" => "07", "Ago" => "08", "Sep" => "09",
                                  "Oct" => "10", "Nov" => "11", "Dec" => "12");

    /**
     * Método que simula os __set
     *
     * @param object $attr
     * @param $value
     * @return void
     */
    public function set($attr, $value) {
        $this->$attr = $value;
    }

    /**
     * Método que simula os __set
     *
     * @param object $attr
     * @param $value
     * @return void
     */
    public function setNull($attr) {
        unset($this->$attr);
    }

    /**
     * Método que simula os __get
     *
     * @param object $attr
     * @return atributo
     */
    public function get($attr) {
        return $this->$attr;
    }

    /**
     * @param Model $attr
     * @return Date
     */
    public function getDate($attr) {
        $formato      = "(\d{4})[-\/]( \d|\d\d)[-\/](\d{2})";
        $formatoLongo = "(\w{3}) ( \d|\d\d) (\d{4})";

        /*
         * Verificamos a existência de uma data nos formatos:
         * dd-mm-yyyy ou dd/mm/yyyy
         */
        if (preg_match("/^$formato$/", $this->$attr, $data)) {
            $data[3] = preg_replace("/ /", "0", $data[3]);
            $value = sprintf("%s/%s/%s", $data[3], $data[2], $data[1]);
        } else if (preg_match("/^$formatoLongo/", $this->$attr, $data)) {
            $data[2] = preg_replace("/ /", "0", $data[2]);
            $value = sprintf("%s/%s/%s", $data[2], self::$MESES[$data[1]], $data[3]);
        } else {
            $value = $this->$attr;
        }
        
        return $value;
    }

    /**
     * Retorna um campo data (no formato dd-mm-yy e, opcionalmente, a hora.
     *
     * @param Model $attr
     * @return Date
     */
    public function getDateTime($attr) {
        $formatoCurto    = "(\d{4})[-\/](\d{2})[-\/](\d{2})";
        $formatoLongo    = "$formatoCurto (\d{2}):(\d{2}):(\d{2})";
        $formatoCompleto = "(\w{3}) ( \d|\d{2}) (\d{4}) (\d{2}):(\d{2}):(\d{2}):(\d*)(AM|PM)";

        /*
         * Verificamos a existência de uma data nos formatos:
         * yyyy-mm-dd ou yyyy/mm/dd
         */
        if (preg_match("/^$formatoCompleto/", $this->$attr, $data)) {
            $data[2] = preg_replace("/ /", "0", $data[2]);
            $value = sprintf("%s/%s/%s %s:%s:%s %s",
                             $data[2], self::$MESES[$data[1]], $data[3],
                             $data[4], $data[5], $data[6], $data[8]);
        } else if (preg_match("/^$formatoLongo$/", $this->$attr, $data)) {
            $value = sprintf("%s/%s/%s %s:%s:%s",
                             $data[3], $data[2], $data[1],
                             $data[4], $data[5], $data[6]);
        } else if (preg_match("/^$formatoCurto$/", $this->$attr, $data)) {
            $value = sprintf("%s/%s/%s", $data[3], $data[2], $data[1]);
        } else {
            $value = $this->$attr;
        }
        
        return $value;
    }

    /**
     * Retorna um campo hora (no formato hh:mm ou hh:mm:ss.
     *
     * @param Model $attr
     * @return Time
     */
    public function getTime($attr) {
        $formatoCurto   = "(\d\d):(\d\d)";
        $formatoLongo   = "$formatoCurto:(\d\d)";
        $formato24horas = "$formatoLongo:(\d\d\d)(AM|PM)";

        /*
         * Verificamos a existência de uma hora nos formatos:
         * hh:mm ou hh:mm:ss
         */
        if (preg_match("/$formato24horas$/", $this->$attr, $data)) {
            $value = sprintf("%s:%s:%s%s", $data[1], $data[2], $data[3], $data[5]);
        } else if (preg_match("/^$formatoLongo/", $this->$attr, $data)) {
            $value = sprintf("%s:%s:%s", $data[1], $data[2], $data[3]);
        } else if (preg_match("/^$formatoCurto/", $this->$attr, $data)) {
            $value = sprintf("%s:%s", $data[1], $data[2]);
        } else {
            $value = $this->$attr;
        }

        return $value;
    }

    public function get_object_vars() {

        return get_object_vars($this);
    }
}
?>